// pages/goodsOrder/goodsOrder.js
//引入事先写好的日期设置util.js文件
var datePicker = require('../../utils/dateSetting.js')

//设定当前的时间，将其设定为常量
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const api = require("../../api/api")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '', //商品id
    radio: '1',
    type: '',
    buyer_id: '',
    sender_id: '',
    img: '',
    price: '',
    title: '',
    people: '',
    phone: '',
    address: '',
    isShow: true,
    ajxtrue: false, //校验电话号码
    time: '',
    // minHour: 10,
    // maxHour: 20,
    // minDate: new Date().getTime(),
    // maxDate: new Date(2019, 10, 1).getTime(),
    // currentDate: new Date().getTime(),
    multiArray: [],
    multiIndex: [0, 0, 0, 0, 0],
    choose_year: "",
  },
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
    console.log(this.data.currentDate)
  },
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
    if (this.data.radio == 1) {
      this.setData({
        isShow: true
      })
    } else {
      this.setData({
        isShow: false
      })
    }
    console.log(this.data.radio)
  },

  onLoad: function (options) {
    this.setData({
      id: options.id, //商品id赋值
    })
    wx.request({
      url: api.getGoodsDetail.url,
      method: api.getGoodsDetail.method,
      data: {
        id: options.id
      },
      success: res => {
        console.log(res)
        var info = res.data.data[0].img.split('|')
        console.log(info)
        this.setData({
          buyer_id: app.globalData.id,
          sender_id: res.data.data[0].senderId,
          img: info[0],
          price: parseInt(res.data.data[0].price) * 100,
          title: res.data.data[0].name
        })

      }
    })
    this.setData({
      multiArray: [
        [year + "年", year + 1 + "年", year + 2 + "年"],
        datePicker.determineMonth(),
        datePicker.determineDay(year, month),
        datePicker.determineHour(),
        datePicker.determineMinute()
      ],
    })
  },
  //最后呈现时间的函数。
  bindMultiPickerChange: function (e) {
    var dateStr = this.data.multiArray[0][this.data.multiIndex[0]] +
      this.data.multiArray[1][this.data.multiIndex[1]] +
      this.data.multiArray[2][this.data.multiIndex[2]] +
      this.data.multiArray[3][this.data.multiIndex[3]] +
      this.data.multiArray[4][this.data.multiIndex[4]];
    this.setData({
      time: dateStr
    })
  },
  //当时间选择器呈现并进行滚动选择时间时调用该函数。
  bindMultiPickerColumnChange: function (e) {
    //e.detail.column记录哪一行发生改变，e.detail.value记录改变的值（相当于multiIndex）
    switch (e.detail.column) {
      //这里case的值有0/1/2/3/4,但除了需要记录年和月来确定具体的天数外，其他的都可以暂不在switch中处理。
      case 0:
        //记录改变的年的值
        let year = this.data.multiArray[0][e.detail.value];
        this.setData({
          choose_year: year.substring(0, year.length - 1)
        })
        break;
      case 1:
        //根据选择的年与月，确定天数，并改变multiArray中天的具体值
        let month = this.data.multiArray[1][e.detail.value];
        let dayDates = datePicker.determineDay(this.data.choose_year, month.substring(0, month.length - 1));
        //这里需要额外注意，改变page中设定的data，且只要改变data中某一个值，可以采用下面这种方法
        this.setData({
          ['multiArray[2]']: dayDates
        })
        break;
    }
    //同上，上面改变的是二维数组中的某一个一维数组，这个是改变一个一维数组中某一个值，可供参考。
    this.setData({
      ["multiIndex[" + e.detail.column + "]"]: e.detail.value
    })
  },

  peopleInput(event) {
    this.setData({
      people: event.detail
    })
  },
  phoneInput(event) {
    var phone = event.detail
    let that = this
    if (!(/^1[34578]\d{9}$/.test(phone))) {

      this.setData({
        ajxtrue: false
      })

    } else {
      this.setData({
        ajxtrue: true
      })
      console.log('验证成功', that.data.ajxtrue)
    }


    this.setData({
      phone: event.detail
    })

  },
  addressInput(event) {
    this.setData({
      address: event.detail
    })
  },
  onSubmit() {
    if (this.data.radio == 1) {
      if (this.data.time == '') {
        wx.showToast({
          title: '请选择自提时间',
          icon: 'none',
          duration: 2000
        })
        return
      }
    } else {
      if (!this.data.people) {
        wx.showToast({
          title: '请输入姓名',
          icon: 'none',
          duration: 2000
        })
        return
      }
      if (!this.data.phone) {
        wx.showToast({
          title: '请输入电话号码',
          icon: 'none',
          duration: 2000
        })
        return
      }
      if (!this.data.address) {
        wx.showToast({
          title: '请输入收货地址',
          icon: 'none',
          duration: 2000
        })
        return
      }
      let that = this
      let ajxtrue = this.data.ajxtrue
      if (ajxtrue == true) {
        //表单提交进行
      } else {
        wx.showToast({
          title: '手机号有误',
          icon: 'none',
          duration: 2000
        })
        return
      }
    }
    wx.request({
      url: api.buyGoods.url,
      method: api.buyGoods.method,
      data: {
        id: this.data.id,
        buyer_id: this.data.buyer_id,
        sender_id: this.data.sender_id,
        type: this.data.radio,
        phone: this.data.phone,
        name: this.data.people,
        address: this.data.address,
        time: this.data.time
      },
      success: res => {
        wx.reLaunch({
          url: '../../pages/index/index',
        })
      }
    })
  },
})